<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Course extends Model
{
    use HasFactory;
    use SoftDeletes;
    protected $fillable = ['adminuser_id','title','desc','sort','image'];
    // 定义需要保护的字段，api请求不到
    protected $hidden = ['created_at','updated_at','deleted_at'];

    // 定义访问器
    public function getImageLinkAttribute(){
        // 这里我们定义一个默认展示
        if(!$this->image){
        return asset("storage/"."/doc/moren.png");

        }
        return asset("storage/".$this->image);
    }

    // 定义一对多资源关联
    public function chapter(){
        return $this->hasMany('App\Models\Chapter')->orderBy('sort','asc');
    }
}
